'use strict'

const { Controller } = require('egg')

class admin extends Controller {
    async create() {
        const { ctx } = this
        console.log(ctx.request.body)
        const {
            title,
            discription,
            link,
            type,
            cover
        } = ctx.request.body
        ctx.set('Access-Control-Allow-Origin', '*')
        if (!await ctx.validate('createPro', ctx.request.body)) return

        await ctx.app.mysql.insert('xuanchuan', {
            title,
            discription,
            link,
            type,
            cover:cover.toString()
        })

        ctx.returnBody(200, '添加项目成功')
    }
    async index() {
        const { ctx } = this

        const result = await ctx.app.mysql.select('xuanchuan')

        ctx.returnBody(200, '获得全部列表成功', result)
    }
    async show() {
        const { ctx } = this
        const { id: type } = ctx.params
        const {
            pageNow = 0,
            pageSize = 10
        } = ctx.query

        if (!await ctx.validate('getWantList', { type, pageNow, pageSize })) return

        const returnData = await ctx.app.mysql.beginTransactionScope(async conn => {
            const totalNum = await conn.select('xuanchuan', {
                columns: ['id'],
                where: { type }
            })
            const datalist = await conn.select('xuanchuan', {
                where: { type },
                limit: parseInt(pageSize),
                offset: parseInt(pageNow),
                orders: [
                    ['list_id', 'asc'],
                    ['id', 'desc']
                ]
            })
            return {
                totalNum: totalNum.length,
                options: { pageSize, pageNow },
                datalist
            }
        }, ctx)

        ctx.returnBody(200, `获得${type}列表成功`, returnData)
    }

    async destroy() {
        const { ctx } = this
        const { id } = ctx.params

        if (!await ctx.validate('idVerify', { id })) return

        const result = await ctx.app.mysql.delete('xuanchuan', { id })

        if (result.affectedRows === 1) {
            ctx.returnBody(200, `项目ID: ${id} 删除成功`)
        } else {
            ctx.returnBody(404, `项目ID: ${id} 不存在`)
        }
    }
    async update() {
        const { ctx, app } = this
        const { id } = ctx.params

        if (!await ctx.validate('createPro', ctx.request.body)) return
        if (!await ctx.validate('idVerify', { id })) return

        const result = await app.mysql.update('xuanchuan', ctx.request.body, {
            where: { id }
        })

        if (result.affectedRows === 1) {
            ctx.returnBody(200, `项目ID: ${id} 修改成功`)
        } else {
            ctx.returnBody(404, `项目ID: ${id} 不存在`)
        }
    }

    async edit() {
        const { ctx, app } = this
        const { id } = ctx.params

        if (!await ctx.validate('idVerify', { id })) return

        const [result] = await app.mysql.select('xuanchuan', {
            where: { id }
        })

        ctx.returnBody(200, `项目ID: ${id} 信息获取成功`, { ...result })
    }

    async reList() {
        const { ctx, app } = this
        let { listData, type } = ctx.request.body
        listData = JSON.parse(listData)
        let sql = 'UPDATE xuanchuan SET list_id = CASE id '
        for (const temp of listData) {
            sql += ` WHEN ${temp.id} THEN ${temp.list_id} `
        }
        sql += ` END WHERE type = '${type}' `
        const result = await app.mysql.query(sql)
        ctx.returnBody(200, '排序成功', result);
    }
}

module.exports = admin